package com.doudizu.shoesonline.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doudizu.shoesonline.constant.CartConstant;
import com.doudizu.shoesonline.mapper.CartMapper;
import com.doudizu.shoesonline.model.entity.Cart;
import com.doudizu.shoesonline.model.entity.Product;
import com.doudizu.shoesonline.model.vo.CartDetailVO;
import com.doudizu.shoesonline.service.CartService;
import com.doudizu.shoesonline.service.LinkService;
import com.doudizu.shoesonline.service.ProductService;
import com.doudizu.shoesonline.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* 
* @description 针对表【cart(购物车)】的数据库操作Service实现
* @createDate 2023-07-03 09:55:24
*/
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart>
    implements CartService {

    @Resource
    private UserService userService;

    @Resource
    private ProductService productService;

    @Resource
    private LinkService linkService;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<CartDetailVO> getCart() {
        List<CartDetailVO> tempList = null;
        String key = CartConstant.CART_PRODUCT_LIST +userService.getLoginUser().getId();
        tempList = (List<CartDetailVO>) redisTemplate.opsForValue().get(key);
        if (tempList != null && !tempList.isEmpty()) {
            return tempList;
        }

        QueryWrapper<Cart> cartQueryWrapper = new QueryWrapper<>();
        cartQueryWrapper.eq("user_id",userService.getLoginUser().getId());
        cartQueryWrapper.gt("product_num",0);
        List<Cart> cartList = this.list(cartQueryWrapper);
        tempList = cartList.stream().map(cart -> {
            CartDetailVO cartDetailVO = new CartDetailVO();
            BeanUtil.copyProperties(cart, cartDetailVO);
            Integer productId = cart.getProductId();
            Product product = productService.getById(productId);
            cartDetailVO.setCartId(cart.getId());
            cartDetailVO.setProductName(product.getProductName());
            cartDetailVO.setPrice(product.getPostPrice());
            String pictureLinkIds = product.getPictureLinkId();
            String[] idArray = pictureLinkIds.split(",");
            List<Integer> list = Arrays.stream(idArray).map(Integer::valueOf).collect(Collectors.toList());
            List<String> urlList = list.stream().map(item -> linkService.getById(item).getUrl()).collect(Collectors.toList());
            cartDetailVO.setPictureLink(urlList);
            return cartDetailVO;
        }).collect(Collectors.toList());
        redisTemplate.opsForValue().set(key,tempList,1, TimeUnit.HOURS);
        return tempList;
    }

    @Override
    public List<CartDetailVO> getCartByIds(List<Integer> ids) {
        List<CartDetailVO> tempList = null;
        List<Cart> carts = this.listByIds(ids);
        tempList = carts.stream().map(cart -> {
            CartDetailVO cartDetailVO = new CartDetailVO();
            BeanUtil.copyProperties(cart, cartDetailVO);
            Integer productId = cart.getProductId();
            Product product = productService.getById(productId);
            cartDetailVO.setCartId(cart.getId());
            cartDetailVO.setProductName(product.getProductName());
            cartDetailVO.setPrice(product.getPostPrice());
            String pictureLinkIds = product.getPictureLinkId();
            String[] idArray = pictureLinkIds.split(",");
            List<Integer> list = Arrays.stream(idArray).map(Integer::valueOf).collect(Collectors.toList());
            List<String> urlList = list.stream().map(item -> linkService.getById(item).getUrl()).collect(Collectors.toList());
            cartDetailVO.setPictureLink(urlList);
            return cartDetailVO;
        }).collect(Collectors.toList());
        return tempList;
    }
}




